local test = UnitTest:New("01DongleStub")

function test:donglestub_register()
	local lib = {}
	function lib:GetVersion()
		return "AlphaLib", 1
	end

	assert_fails(function() DongleStub("AlphaLib") end, "Cannot find a library with name 'AlphaLib'")
	DongleStub:Register(lib)
	assert(DongleStub("AlphaLib") == lib)

	local lib = {}
	assert_fails(function() DongleStub:Register(lib) end, "Attempt to register a library with DongleStub that does not have a 'GetVersion' method.")
	function lib:GetVersion()
		return 1
	end
	assert_fails(function() DongleStub:Register(lib) end, "Attempt to register a library with DongleStub that does not have a proper major version.")
	function lib:GetVersion()
		return "MajorVersion"
	end
	assert_fails(function() DongleStub:Register(lib) end, "Attempt to register a library with DongleStub that does not have a proper minor version.")
end

function test:donglestub_newversion()
	local lib = {}
	function lib:GetVersion()
		return "BetaLib",1
	end

	assert(DongleStub:IsNewerVersion(lib:GetVersion()))
	lib = DongleStub:Register(lib)
	assert(DongleStub("BetaLib") == lib)
	assert(select(2,DongleStub("BetaLib"):GetVersion()) == 1)

	local lib2 = {}
	function lib2:GetVersion()
		return "BetaLib",0
	end

	assert(not DongleStub:IsNewerVersion(lib2:GetVersion()))
	lib2 = DongleStub:Register(lib2)
	assert(DongleStub("BetaLib") == lib)
	assert(select(2,DongleStub("BetaLib"):GetVersion()) == 1)

	local lib3 = {}
	function lib3:GetVersion()
		return "BetaLib",2
	end

	assert(DongleStub:IsNewerVersion(lib3:GetVersion()))
	lib3 = DongleStub:Register(lib3)
	assert(DongleStub("BetaLib") == lib3)
	assert(select(2,DongleStub("BetaLib"):GetVersion()) == 2)
end

function test:donglestub_activate()
	local lib = {}
	function lib:GetVersion()
		return "GammaLib", 1
	end

	local function activate(new, old)
		assert(new == lib)
		assert(old == nil)
		lib.activate = true
	end

	assert(DongleStub:IsNewerVersion(lib:GetVersion()))
	lib = DongleStub:Register(lib, activate)

	local lib2 = {}
	function lib2:GetVersion()
		return "GammaLib", 2
	end

	local function activate2(new, old)
		assert(new == lib2)
		assert(old == lib)
		lib2.activate = true
	end

	assert(DongleStub:IsNewerVersion(lib2:GetVersion()))
	lib2 = DongleStub:Register(lib2, activate2)

	local lib3 = {}
	function lib3:GetVersion()
		return "GammaLib", 3
	end

	local function activate3(new, old)
		assert(new == lib3)
		assert(old == lib2)
		lib3.activate = true
	end

	assert(DongleStub:IsNewerVersion(lib3:GetVersion()))
	lib3 = DongleStub:Register(lib3, activate3)
	assert(lib.activate)
	assert(lib2.activate)
	assert(lib3.activate)
end

function test:donglestub_deactivate()
	local lib = {}
	local lib2 = {}

	function lib:GetVersion()
		return "DeltaLib", 1
	end

	local olddeact
	local function deactivate(old, new)
		assert(old == lib)
		assert(new == lib2)
		olddeact = true
	end

	lib = DongleStub:Register(lib, activate, deactivate)

	function lib2:GetVersion()
		return "DeltaLib", 2
	end

	local newdeact
	local function deactivate2(new, old)
		assert(new == lib2)
		assert(old == lib)
		newdeact = true
	end

	assert(DongleStub:IsNewerVersion(lib2:GetVersion()))
	lib2 = DongleStub:Register(lib2, activate2, deactivate2)
	assert(olddeact == true)
	assert(newdeact == nil)
end

test:Run()